<template>
  <div class="star-header-right">
    <div class="star-user-info">
      <el-dropdown>
        <span class="el-dropdown-link">
          <el-icon class="el-icon--left"><User /></el-icon>
          {{ userStore.username }}
          <el-icon class="el-icon--right"><arrow-down /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item icon="SwitchButton" @click="handleLoginOut">注销</el-dropdown-item>
            <el-dropdown-item icon="Refresh" @click="handleRefresh">刷新缓存</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import router from '@/router'
import useStore from '@/store'
import { ElMessage } from 'element-plus'

const { userStore, menuStore } = useStore()

const handleLoginOut = () => {
  userStore.logout().then(() => {
    ElMessage({
      message: '登出成功!',
      type: 'success',
      duration: 800
    })
    router.push({
      name: 'login'
    })
  })
}

const handleRefresh = () => {
  menuStore.RESET_STATE()
  ElMessage({
    message: '缓存刷新成功!',
    type: 'success',
    duration: 800
  })
  router.go(0)
}

</script>

<style lang="less" scoped>
.star {
  &-header-right {
    height: 100%;
  }
  &-user-info {
    float: right;
    width: 110px;
    height: 100%;
    margin-right: 20px;
    text-align: center;
  }
  &-user-info:hover {
    background: var(--el-color-info-light-9);
  }
}
.el-dropdown {
  font-size: 16px;
  padding-top: 22px;
}
.el-dropdown-link {
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
}
</style>
